package com.readbook.service;

import com.readbook.dao.ScdhDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ScdhService {
    @Autowired
    private ScdhDao scdhDao;

    public List<Map<String, Object>> Search_poetry(Map<String, Object> gmap) {
        List<Map<String, Object>> list1 = scdhDao.Search_poetry(gmap);
        return list1;
    }


    public Map<String, Object> del_poetry(Map<String, Object> gmap) {
        Map<String, Object> smap = new HashMap<>();
        scdhDao.del_poetry(gmap);
        smap.put("result", true);
        smap.put("msg", "删除成功!");
        return smap;
    }


    public int add_poetry(Map<String, Object> gmap) {
        return scdhDao.add_poetry(gmap);
    }

    public int edit_poetry(Map<String, Object> gmap) {
        return scdhDao.edit_poetry(gmap);
    }


    public int close_otherpoetry(Map<String, Object> gmap) {
        return scdhDao.close_otherpoetry(gmap);
    }


    public void set_poetryJf(Map<String, Object> gmap) {
        String totalscore = gmap.get("totalscore").toString();
        String maxpm = gmap.get("maxpm").toString();
        int total = Integer.parseInt(totalscore);
        int max = Integer.parseInt(maxpm);
        int dj = total / max;

        //查询参加该诗词大会的所有年级
        List<Map<String, Object>> listnj = new ArrayList<>();
        listnj = scdhDao.cjscdhAllNj(gmap);
        //查询每个年级的前十名
        for (int i = 0; i < listnj.size(); i++) {
            Map<String, Object> njmap = listnj.get(i);
            String nj = njmap.get("NJ").toString();
            Map<String, Object> setmap = new HashMap<String, Object>();
            setmap.put("nj", nj);
            setmap.put("pid", gmap.get("pid"));
            List<Map<String, Object>> listmap = new ArrayList<>();
            listmap = scdhDao.getTenNumberStu(setmap);
            int zgf = total;
            for (int j = 0; j < listmap.size(); j++) {
                //记录积分
                Map<String, Object> jfmap = new HashMap<String, Object>();
                String jfid = UUID.randomUUID().toString().replaceAll("-", "");
                jfmap.put("jfid", jfid);
                jfmap.put("lyid", gmap.get("pid"));
                jfmap.put("jfly", "poetry");//积分来源 打卡
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                String addtime = df.format(new Date());
                jfmap.put("addtime", addtime);
                jfmap.put("usid", listmap.get(j).get("USID"));//谁的积分
                jfmap.put("username", listmap.get(j).get("NAME"));
                jfmap.put("jfnum", zgf);//所得积分
                scdhDao.savejfjl(jfmap);
                zgf = zgf - dj;
            }
        }
    }


    /**
     * @author 牟明艳
     * @comments 分页查询诗词排名
     * @time 2019年10月22日00:15:54
     */
    public List<Map<String, Object>> Search_ScdhPm_page(Map<String, Object> gmap) {
        List<Map<String, Object>> listb = new ArrayList<>();
        listb = scdhDao.Search_ScdhPm_page(gmap);
        return listb;
    }


    /**
     * @author 牟明艳
     * @comments 分页查询诗词排名统计
     * @time 2019年10月22日00:15:54
     */
    public int Search_ScdhPm_count(Map<String, Object> gmap) {
        return scdhDao.Search_ScdhPm_count(gmap);
    }

}


